class Admin::TeamsController < AdminController
  def index
    @teams = Team.all
    if params[:search_key].presence
      @teams = @teams.where("name LIKE ?", "%#{params[:search_key]}%")
    end
    @teams = @teams.order("created_at DESC").page params[:page]
  end

  def new
    @team = Team.new
  end

  def create
    @team = Team.new team_params
    if @team.save
      redirect_to admin_teams_path, notice: "保存成功！"
    else
      redirect_to admin_teams_path, notice: "保存失败！"
    end
  end

  def edit
    @team = Team.find params[:id]
  end

  def update
    @team = Team.find params[:id]
    if @team.update team_params
      redirect_to admin_teams_path, notice: "更新成功！"
    else
      redirect_to admin_teams_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @team = Team.find params[:id]
    if @team.destroy
      redirect_to admin_teams_path, notice: "删除成功."
    else
      redirect_to admin_teams_path, notice: "删除失败."
    end
  end

  private

  def team_params
    params.require(:team).permit(:name)
  end
end
